package com.huatai.datacenter.controller.dataquality;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.huatai.common.api.R;
import com.huatai.datacenter.entity.dataquality.QualityRuleEntity;
import com.huatai.datacenter.service.QualityRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author gusiyi
 * @version 1.0
 * @className QualityRuleController
 * @description 描述：
 * @date 2023/6/2 9:19
 */
@RestController
@RequestMapping("/dataQuality/rule")
@Api(value = "数据质量规则", tags = "数据质量规则")
public class QualityRuleController {

	@Autowired
	private QualityRuleService qualityRuleService;

	@RequestMapping("/getList")
	@ApiOperation("获取数据质量规则清册")
	@ApiOperationSupport(order = 1, includeParameters = {
		"qualityRuleEntity.pageIndex",
		"qualityRuleEntity.pageSize",
		"qualityRuleEntity.qualityRuleName"
	})
	public R<Page<QualityRuleEntity>> getList(@RequestBody QualityRuleEntity qualityRuleEntity){
		Page<QualityRuleEntity> dataModelEntityPage = qualityRuleService.selectQualityRuleList(qualityRuleEntity);
		return R.data(dataModelEntityPage,"获取获取数据质量规则成功");
	}

	@RequestMapping("/saveOne")
	@ApiOperation("保存数据质量规则")
	@ApiOperationSupport(order = 2, includeParameters = {
		"qualityRuleEntity.qualityRuleName",
		"qualityRuleEntity.remark",
		"qualityRuleEntity.qualityRuleTypeNum",
		"qualityRuleEntity.qualityRuleTypeName",
		"qualityRuleEntity.methodContent"
	})
	public R<Page<QualityRuleEntity>> insetOne(@RequestBody QualityRuleEntity qualityRuleEntity){
		return R.status(qualityRuleService.insertQualityRule(qualityRuleEntity));
	}

	@RequestMapping("/updateById")
	@ApiOperation("修改数据质量规则")
	@ApiOperationSupport(order = 3, includeParameters = {
		"qualityRuleEntity.id",
		"qualityRuleEntity.qualityRuleName",
		"qualityRuleEntity.remark",
		"qualityRuleEntity.qualityRuleTypeNum",
		"qualityRuleEntity.qualityRuleTypeName",
		"qualityRuleEntity.methodContent"
	})
	public R<Page<QualityRuleEntity>> updateById(@RequestBody QualityRuleEntity qualityRuleEntity){
		qualityRuleEntity.setUpdateTime(null);
		qualityRuleEntity.setUpdateUserid(null);
		qualityRuleEntity.setDeleted(null);
		return R.status(qualityRuleService.updateQualityRule(qualityRuleEntity));
	}

	@RequestMapping("/deleteByIds")
	@ApiOperation("删除数据质量规则")
	@ApiOperationSupport(order = 4, includeParameters = {
		"qualityRuleEntityIds.id"
	})
	public R<Page<QualityRuleEntity>> deleteByIds(@RequestBody List<QualityRuleEntity> qualityRuleEntityIds){
		return R.status(qualityRuleService.deleteByIds(qualityRuleEntityIds));
	}


	@RequestMapping("/checkQualityRuleByFieldName")
	@ApiOperation("通过质量规则id校验数据质量")
	@ApiOperationSupport(order = 99,includeParameters = {
		"checkValue",
		"ruleId",
		"dataModelId",
		"dataModelName",
		"fieldName"
	})
	public boolean checkQualityRuleByFieldName(@RequestBody JSONObject jsonObject){
		return qualityRuleService.checkQualityRuleByFieldName(jsonObject.getString("checkValue"),jsonObject.getLong("ruleId"),
			jsonObject.getLong("dataModelId"),jsonObject.getString("dataModelName"),jsonObject.getString("fieldName"),
			LocalDateTime.now(), LocalDateTime.now());
	}
}
